package ink.sunflowerk.handler;


import ink.sunflowerk.entity.MyDirectory;
import ink.sunflowerk.entity.ResponseVO;
import ink.sunflowerk.service.MyDirectoryService;
import ink.sunflowerk.service.MyFileService;
import ink.sunflowerk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/myDirectory")
public class MyDirectoryAction {
    @Autowired
    private MyDirectoryService myDirectoryService;
    @Autowired
    private MyFileService myFileService;
    @Autowired
    private UserService userService;

    //1 获取子文件/文件夹集合
    @RequestMapping(value = "/getAllByDfu.action", method = RequestMethod.GET)
    public ResponseVO<List<MyDirectory>> getAllByDfuMethod(String dfu, int uid) {
        return new ResponseVO<List<MyDirectory>>(200, "获取成功！", myDirectoryService.getAllByDfu(dfu, uid));
    }

    //2 添加文件夹
    @RequestMapping(value = "/addOne.action", method = RequestMethod.POST)
    public ResponseVO<String> addOneMethod(@RequestBody MyDirectory myDirectory) {
        myDirectory.setDprivate(0);//默认私有
        myDirectory.setDsize(0);//空的
        myDirectory.setDtime(new Date());
        myDirectory.setDtype(0);//文件夹0 文件1
        myDirectoryService.addOne(myDirectory);
        return new ResponseVO<String>(200, "添加文件夹成功！", null);
    }

    //3 删除文件夹
    @RequestMapping(value="/deleteSome/{dids}.action",method = RequestMethod.DELETE)
    public ResponseVO<String> deleteSomeMethod(@PathVariable("dids")String dids){
        dids=dids.substring(0,dids.length()-1);
        String[] didArr=dids.split("-");
        for(String didStr:didArr){
            myDirectoryService.deleteOne(Integer.parseInt(didStr));
        }
        return new ResponseVO<String>(200,"删除文件夹成功！",null);
    }

    //4 修改文件夹
    @RequestMapping(value = "/updateOne.action", method = RequestMethod.PUT)
    public ResponseVO<String> updateOneMethod(@RequestBody MyDirectory myDirectory) {
        myDirectoryService.updateOne(myDirectory);
        return new ResponseVO<String>(200, "修改文件夹成功！", null);
    }
}
